/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */



package itm.dao.productos;

//~--- non-JDK imports --------------------------------------------------------

import itm.exception.ControlException;

import itm.vo.prodructos.AtributosVO;

//~--- JDK imports ------------------------------------------------------------

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;

import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author BISA20
 */
public class AtributosDAO {
    private CallableStatement callStmt = null;
    private ResultSet         res      = null;
    private String            sql      = "";
    Connection                dbConexion;

    public AtributosDAO(Connection dbConexion) {
        this.dbConexion = dbConexion;
    }

    public List<AtributosVO> consultarAtributos(AtributosVO atributosVO) throws ControlException, SQLException {
        List<AtributosVO> listaAtributos = new ArrayList<AtributosVO>();

        try {
            sql      = "{call ITM_CONSULTAR_ATRIBUTOS(?)}";
            callStmt = dbConexion.prepareCall(sql);
            callStmt.setString(1, atributosVO.getIdTipoProducto());
            res = callStmt.executeQuery();

            while (res.next()) {
                atributosVO = new AtributosVO();
                atributosVO.setNombreAtributo(res.getString(1));
                atributosVO.setIdAtributoProducto(res.getString(2));
                listaAtributos.add(atributosVO);
            }
        } catch (SQLException ce) {
            ce.getStackTrace();
        }

        return listaAtributos;
    }

    public List<AtributosVO> consultarAtributosAnteriores(AtributosVO atributosVO)
            throws ControlException, SQLException {
        List<AtributosVO> listaAtributos = new ArrayList<AtributosVO>();

        try {
            sql      = "{call ITM_CONSULTAR_ATRIBUTOS_ANTERIORES(?)}";
            callStmt = dbConexion.prepareCall(sql);
            callStmt.setString(1, atributosVO.getIdProducto());
            res = callStmt.executeQuery();

            while (res.next()) {
                atributosVO = new AtributosVO();
                atributosVO.setNombreAtributo(res.getString(1));
                atributosVO.setValor(res.getString(3));
                atributosVO.setIdAtributoProducto(res.getString(2));
                listaAtributos.add(atributosVO);
            }
        } catch (SQLException ce) {
            ce.getStackTrace();
        }

        return listaAtributos;
    }

    public List<AtributosVO> listarAtribu(AtributosVO atributosVO) throws ControlException, SQLException {
        List<AtributosVO> listaatri = new ArrayList<AtributosVO>();

        try {
            sql      = "{call ITM_LISTA_ATRIBUTOS()}";
            callStmt = dbConexion.prepareCall(sql);
            res      = callStmt.executeQuery();

            while (res.next()) {
                atributosVO = new AtributosVO();
                atributosVO.setNombreAtributo(res.getString(1));

                // categoriaProductosVO.setIdCategoria(res.getString(2));
                listaatri.add(atributosVO);
            }
        } catch (SQLException ce) {
            ce.getStackTrace();
        }

        return listaatri;
    }

    public String insertarAtributos(AtributosVO atributosVO) throws ControlException, SQLException {
        String temp      = "",
               resultado = "";

        try {
            sql      = "{call ITM_INSERTAR_ATRIBUTOS_PRODUCTO(?,?,?,?)}";
            callStmt = dbConexion.prepareCall(sql);
            callStmt.registerOutParameter(1, java.sql.Types.VARCHAR);
            callStmt.setString(2, atributosVO.getIdAtributoProducto());
            callStmt.setString(3, atributosVO.getValor());
            callStmt.setString(4, atributosVO.getIdProducto());
            callStmt.execute();
            resultado = callStmt.getString(1);
            temp      = resultado;
        } catch (SQLException ce) {
            ce.getStackTrace();
        }

        return temp;
    }

    public String modificarAtributos(AtributosVO atributosVO) throws ControlException, SQLException {
        String temp      = "",
               resultado = "";

        try {
            sql      = "{call ITM_MODIFICAR_ATRIBUTOS_PRODUCTO(?,?,?,?)}";
            callStmt = dbConexion.prepareCall(sql);
            callStmt.registerOutParameter(1, java.sql.Types.VARCHAR);
            callStmt.setString(2, atributosVO.getIdAtributoProducto());
            callStmt.setString(3, atributosVO.getValor());
            callStmt.setString(4, atributosVO.getIdProducto());
            callStmt.execute();
            resultado = callStmt.getString(1);
            temp      = resultado;
        } catch (SQLException ce) {
            ce.getStackTrace();
        }

        return temp;
    }

    public String eliminarAtributoProducto(AtributosVO atributosVO) throws ControlException, SQLException {
        String temp = "";

        try {
            sql      = "{call ITM_ELIMINAR_ATRIBUTO_PRODUCTO(?,?)}";
            callStmt = (CallableStatement) dbConexion.prepareCall(sql);
            callStmt.registerOutParameter(1, java.sql.Types.VARCHAR);
            callStmt.setString(2, atributosVO.getIdAtributoProducto());
            callStmt.execute();
            temp = callStmt.getString(1);
        } catch (SQLException ce) {
            ce.printStackTrace();
        }

        return temp;
    }
}
